<?php
/*
	Copyright 2010 Iresha Gunathileke (ireshagun@gmail.com)   
 	
 	Licensed under the Apache License, Version 2.0 (the "License");
 	
 	Changes to the original file include
 	1. Additional functions to read and supply proxies.
 	2. Implemtation of the enpty interfaces.
 	
 	http://code.google.com/p/were-proxy/
 */


/*
	Copyright 2010 Ping Shin Ching (ping_sc@hotmail.com)

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
	
	Project Mugatiya: mugatiya.wordpress.com
 */



/*
 * If true then bypass content filtering - faster response
 * Default add will be 'unclassified'
 */
define('STREAMLINE', true);


/*
 * Arrays and scalars to keep track of the subbed stuff
 */
$subbedurl=0;

$subtracker=array();
$subtrackerbbc=array();
$subtrackermini=array();

$sitecount=0;
$sitecountmini=0;
$sitecountbbc=0;

/*
 * The arrays are now in a seperate file to make updating
 * easier as they are not now always over-wirtten
 */
include "subinsites.php";

/*
 * Called to modify POST Values
 */
function imodpostvalues($url, $host, $post){
	//This function now does nothing;
}


/*
 * Check to see if we have made a change
 */
function changedpost(){
	global $subbedurl;
	return $subbedurl;
}


/*
 * Randomize the arrays for different results
 */
function randomizesubinarrays(){
	global 	$subinsites, $subinsitesbbc, $subinsitesmini;
	
	shuffle($subinsites);
	shuffle($subinsitesbbc);
	shuffle($subinsitesmini);
}


/*
 * Modify the response data and reverse the changes
 */
function imodresponse(&$output){
	global $subtracker, $subtrackerbbc,$subtrackermini, $subbedurl;

	/*
	error_log(print_r($subtracker,1));
	error_log(print_r($subtrackerbbc,1));
	error_log(print_r($subtrackermini,1));
	*/
	
	if($subbedurl){
		foreach($subtracker as $n => $v){
			$output=str_replace($v, $n, $output);	
		}
		foreach($subtrackerbbc as $n => $v){
			$output=str_replace($v, $n, $output);	
		}				
		foreach($subtrackermini as $n => $v){
			$output=str_replace($v, $n, $output);	
		}
	}
	return $output;
}

/*
 * Function to get a valid proxy from a list.
 */
function getaproxy($filename){
	$lines = array();
	if(($f = fopen($filename, 'r'))!==FALSE){
		while(!feof($f)){
			$s=fgets($f, 1024);
			if(preg_match("/\w+/ims", $s))
				array_push($lines, trim($s));
 		}
 		fclose($f);
 		if(count($lines)>0){
 			$lines = array_slice($lines, 0, 10);
 			return $lines[array_rand($lines)];
 		}
 		else
 			return 0; 	
	}
	else{
		return 0;	
	}	
}

/*
 * Modify the posted changes at the postrequest.php getencodedpost() function
 */
function postmodify($post){
	global $subtracker, $subtrackerbbc,$subtrackermini, 
	$subinsites, $subinsitesbbc, $subinsitesmini,
	$sitecount,	$sitecountmini,	$sitecountbbc,
	
	$subbedurl;
	
	if(preg_match_all("/<\s*a\s+href\s*=\s*\"?http:\/\/[\w\.\-\#\/=\&\?]+\"?>[^<>\"]+<\s*\/\s*a>/ims", $post, $matches)){
		$unique = array_merge(array(),array_unique($matches[0]));

		for( $i=0; $i < count($unique) ; $i++){
			$subbedurl=1;
			if(isset($subtracker[$unique[$i]])){
				$selectedsite=$subtracker[$unique[$i]];
			}
			else{
				if($sitecount<count($subinsites)){
					$subtracker[$unique[$i]]=$subinsites[$sitecount];	
					$sitecount++;
				}
				else{
				;
				}
			}
		}
		foreach($subtracker as $l => $nl){
			$post = str_replace($l, $nl, $post);
		}
		
	}
	else if(preg_match_all("/\[url=http:\/\/[\w\.\-\#\/=\&\?]+\"?\][^\[\]]+\[\/url\]/ims", $post, $matches)){

		$unique = array_merge(array(),array_unique($matches[0]));

		for( $i=0; $i < count($unique) ; $i++){
			$subbedurl=1;
			if(isset($subtrackerbbc[$unique[$i]])){
				$selectedsite=$subtrackerbbc[$unique[$i]];
			}
			else{
				if($sitecountbbc<count($subinsitesbbc)){
					$subtrackerbbc[$unique[$i]]=$subinsitesbbc[$sitecountbbc];	
					$sitecountbbc++;
				}
				else{
				;
				}
			}
		}
	
		foreach($subtrackerbbc as $l => $nl){
			$post = str_replace($l, $nl, $post);
		}
		
	}
	else if(preg_match_all("/http:\/\/[\w\.\-\#\/=\&\?]+/ims", $post, $matches)){

		$unique = array_merge(array(),array_unique($matches[0]));

		for( $i=0; $i < count($unique) ; $i++){
			$subbedurl=1;
			if(isset($subtrackermini[$unique[$i]])){
				$selectedsite=$subtrackermini[$unique[$i]];
			}
			else{
				if($sitecountmini<count($subinsitesmini)){
					$subtrackermini[$unique[$i]]=$subinsitesmini[$sitecountmini];	
					$sitecountmini++;
				}
				else{
				;
				}
			}
		}
		
		foreach($subtrackermini as $l => $nl){
			$post = str_replace($l, $nl, $post);
		}
	}
		
	
	else{
		;
	}

	return $post;
}
?>